﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Mukhopatro.BusinessLogicLayer;
using Mukhopatro.Helpers;
using Mukhopatro.BusinessEntities;

namespace Mukhopatro.Admin
{
    public partial class settings : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
               LoadData();
            }
        }

        private void LoadData()
        {
            SiteUtility utility = new SiteUtility();
            ddlCompanyCountry.DataSource = utility.GetCountries();
            ddlCompanyCountry.DataTextField = "Key";
            ddlCompanyCountry.DataValueField = "Value";
            ddlCompanyCountry.DataBind();

            CustomerSettingBLL bll = new CustomerSettingBLL();
            CustomerSetting settings=bll.GetCustomerSettings(SessionManager.CustomerId);
            if (settings != null)
            {
                tbxAdminEmail.Text = settings.AdminEmail;
                tbxCompanyAddress.Text = settings.CompanyAddress;
                tbxCompanyCity.Text = settings.CompanyCity;
                tbxCompanyEmail.Text = settings.CompanyEmail;
                tbxCompanyName.Text = settings.CompanyName;
                tbxCompanyPhone.Text = settings.CompanyPhone;
                tbxCompanyZip.Text = settings.CompanyZip;
                ddlCompanyCountry.SelectedIndex = ddlCompanyCountry.Items.IndexOf(ddlCompanyCountry.Items.FindByValue(settings.CompanyCountry));
                CKEditorSales.Text = settings.SalesInquiry;
                CKEditorCustomerSupport.Text = settings.CustomerSupport;
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            CustomerSettingBLL bll = new CustomerSettingBLL();
            CustomerSetting settings = bll.GetCustomerSettings(SessionManager.CustomerId);
            if (settings != null)
            {
                settings.AdminEmail=tbxAdminEmail.Text.Trim();
                settings.CompanyAddress=tbxCompanyAddress.Text.Trim();
                settings.CompanyCity=tbxCompanyCity.Text.Trim();
                settings.CompanyEmail=tbxCompanyEmail.Text;
                settings.CompanyName=tbxCompanyName.Text.Trim();
                settings.CompanyPhone=tbxCompanyPhone.Text.Trim();
                settings.CompanyZip=tbxCompanyZip.Text;
                settings.CompanyCountry = ddlCompanyCountry.SelectedItem.Text;
                settings.SalesInquiry = CKEditorSales.Text;
                settings.CustomerSupport = CKEditorCustomerSupport.Text;
                try
                {
                    bll.UpdateCustomerSetting(settings);
                    respMessage.SetSuccessMessage("Update successful.");
                }
                catch(Exception ex)
                {
                    respMessage.SetFailMessage("Failed to update."+ex.Message);
                }

            }
        }
    }
}